home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / SearchProcs & Color Sep / SearchProcs and Color Sep.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.3 KB  |  281 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        SearchProcs and Color Sep.c
  3.  
  4.     Contains:    This snippet shows how to create and install your        
  5.                 own custom search procedure.  In this example, the        
  6.                 searchProc performs RGB color separation by checking    
  7.                 to see if the individual RGB component values fall        
  8.                 within a certain maximum and minimum threshold level.    
  9.  
  10.     Written by: Edgar Lee    
  11.  
  12.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 7/14/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 
  25.  
  26. */
  27. /****************************************************************************/
  28.  
  29. #include <Types.h>
  30. #include <Resources.h>
  31. #include <QuickDraw.h>
  32. #include <Events.h>
  33. #include <Windows.h>
  34. #include <Quickdraw.h>
  35. #include <QDOffscreen.h>
  36. #include <Fonts.h>
  37. #include <TextEdit.h>
  38. #include <Dialogs.h>
  39.  
  40.  
  41. /* Constant Declarations */
  42.  
  43. #define    WWIDTH            500
  44. #define    WHEIGHT            185
  45.  
  46. #define WLEFT            (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  47. #define WTOP            (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  48.  
  49. #define kHiliteCutoff    60000            /* threshold levels */
  50. #define kShadowCutoff    9000            /* threshold levels */
  51.  
  52. enum {
  53.     tRedSeparation = 0,
  54.     tGreenSeparation,
  55.     tBlueSeparation
  56. };
  57.  
  58. /* Global Variable Definitions */
  59.  
  60. WindowPtr        gWindow;
  61. short            gColorType;
  62.  
  63. void initMac();
  64. void createWindow();
  65. void doEventLoop();
  66.  
  67. void doColorSeparation();
  68. static pascal Boolean searchProc();
  69.  
  70. void main()
  71. {
  72.     initMac();
  73.     createWindow();
  74.     doEventLoop();
  75. }
  76.  
  77. void initMac()
  78. {
  79.     MaxApplZone();
  80.     
  81.     InitGraf( &qd.thePort );
  82.     InitFonts();
  83.     InitWindows();
  84.     InitMenus();
  85.     TEInit();
  86.     InitDialogs( nil );
  87.     InitCursor();
  88.     FlushEvents( 0, everyEvent );
  89. }
  90.  
  91. void createWindow()
  92. {
  93.     Rect    rect;
  94.     
  95.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  96.     
  97.     gWindow = NewCWindow( 0L, &rect, "\pSearchProcs & Color Separation", true, documentProc,
  98.                             (WindowPtr)-1L, true, 0L );
  99.                             
  100.     SetPort( gWindow );
  101. }
  102.  
  103. void doColorSeparation()
  104. {
  105.     short            i;
  106.     PicHandle        pict;                /* Pict used for the source. */
  107.     GWorldPtr        source;                /* Gworld used to store the pict. */
  108.     PixMapHandle    sourcePixMap;        /* Handle to the source pixmap. */
  109.     Rect            rect;                /* Bounding rect of source and destination. */
  110.     CGrafPtr        currentPort;        /* Saved CGrafPtr for later restore. */
  111.     GDHandle        currentDevice;        /* Saved device for later restore. */
  112.     ColorSearchUPP    searchProcUPP=NewColorSearchProc(searchProc);
  113.     
  114.     /* Load the pict resource to be used for the source. */
  115.     pict = (PicHandle)GetResource( 'PICT', 128 );
  116.  
  117.     rect = (**pict).picFrame;
  118.     OffsetRect( &rect, -rect.left, -rect.top );
  119.     
  120.     /* Draw the source image in the window to see what it looks like. */
  121.     DrawPicture( pict, &rect );
  122.     HPurge( (Handle)pict );
  123.  
  124.     /* Create a gworld to store the pict. */
  125.     NewGWorld( &source, 8, &rect, GetCTable( 8 + 72 ), nil, 0 );
  126.     sourcePixMap = GetGWorldPixMap( source );
  127.     
  128.     /* Draw the pict into the gworld. */
  129.     GetGWorld( ¤tPort, ¤tDevice );
  130.     SetGWorld( source, nil );
  131.     
  132.     DrawPicture( pict, &rect );
  133.     SetGWorld( currentPort, currentDevice );
  134.     
  135.     /* Do the color separation using the custom searchProcs. */
  136.     
  137.     AddSearch(searchProcUPP);
  138.     
  139.     for (i = 0; i < 3; i++)
  140.     {
  141.         OffsetRect( &rect, (**sourcePixMap).bounds.right, 0 );
  142.         gColorType = i;
  143.  
  144.         /* Get a new seed for the offscreen ctable to insure that the searchProc is called. */
  145.         (**(**sourcePixMap).pmTable).ctSeed = GetCTSeed();
  146.         
  147.         /* Copy the source to the window while using the custom searchProc. */
  148.         CopyBits( (BitMap *)*sourcePixMap, &gWindow->portBits,
  149.                     &(**sourcePixMap).bounds, &rect, srcCopy, 0l );
  150.     }
  151.     
  152.     DelSearch(searchProcUPP);
  153.     
  154.     /* Release the offscreen memory. */
  155.     DisposeGWorld( source );
  156. }
  157.  
  158. static pascal Boolean searchProc(RGBColor* color, long* position )
  159. {
  160.     #pragma unused(position)
  161.     RGBColor    allWhite = { 0xffff, 0xffff, 0xffff };
  162.     RGBColor    allBlack = { 0, 0, 0 };
  163.     
  164.     if( gColorType == tRedSeparation )
  165.     {    
  166.             /* hilite range */
  167.         if( color->red > kHiliteCutoff )
  168.         {
  169.             /* all colors go to white */
  170.             *color = allWhite;
  171.         }
  172.         
  173.             /* shadow range */
  174.         else if( color->red < kShadowCutoff )
  175.         {
  176.             /* all colors go to black */
  177.             *color = allBlack;
  178.         }
  179.             /* middle range */
  180.             
  181.         else
  182.         {
  183.             /* just show red component */
  184.             color->green = 0;
  185.             color->blue = 0;
  186.         }
  187.     }
  188.     else if( gColorType == tGreenSeparation )
  189.     {
  190.             /* hilite range */
  191.         if( color->green > kHiliteCutoff )
  192.         {
  193.             /* all colors go to white */
  194.             *color = allWhite;
  195.         }
  196.                     
  197.             /* shadow range */
  198.         else if( color->green < kShadowCutoff )
  199.         {
  200.             /* all colors go to black */
  201.             *color = allBlack;
  202.         }
  203.         
  204.             /* middle range */
  205.         else
  206.         {
  207.             /* show green component */
  208.             color->red = 0;
  209.             color->blue = 0;
  210.         }
  211.         
  212.     }
  213.     else if( gColorType == tBlueSeparation )
  214.     {
  215.             /* hilite range */
  216.         if( color->blue > kHiliteCutoff )
  217.         {
  218.             /* all colors go to white */
  219.             *color = allWhite;
  220.         }
  221.         
  222.             /* shadow range */
  223.         else if( color->blue < kShadowCutoff )
  224.         {
  225.             /* all colors go to black */
  226.             *color = allBlack;
  227.         }
  228.         
  229.             /* middle range */
  230.         else
  231.         {
  232.             /* show greens */
  233.             color->red = 0;
  234.             color->green = 0;
  235.         }
  236.     }
  237.     
  238.     return false;
  239. }
  240.  
  241. void doEventLoop()
  242. {
  243.     EventRecord        event;
  244.     WindowPtr        window;
  245.     short            clickArea;
  246.     Rect              screenRect;
  247.     
  248.     for (;;)
  249.     {
  250.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  251.         {
  252.             if (event.what == mouseDown)
  253.             {
  254.                 clickArea = FindWindow( event.where, &window );
  255.                 
  256.                 if (clickArea == inDrag)
  257.                 {
  258.                     screenRect = (**GetGrayRgn()).rgnBBox;
  259.                     DragWindow( window, event.where, &screenRect );
  260.                 }
  261.                 else if (clickArea == inContent)
  262.                 {
  263.                     if (window != FrontWindow())
  264.                         SelectWindow( window );
  265.                 }
  266.                 else if (clickArea == inGoAway)
  267.                     if (TrackGoAway( window, event.where ))
  268.                         return;
  269.             }
  270.             else if (event.what == updateEvt)
  271.             {
  272.                 window = (WindowPtr)event.message;    
  273.                 SetPort( window );
  274.                 
  275.                 BeginUpdate( window );
  276.                 doColorSeparation();
  277.                 EndUpdate( window );
  278.             }
  279.         }
  280.     }
  281. }